/*
 * merge_sort.c
 *
 *  Created on: Mar 25, 2014
 *      Author: jack
 */
#include<stdio.h>

void merge_sort(int sarr[],int tarr[], int start,int end) {
	int s=start,e=end;
	if(start==end) {
		tarr[start]=sarr[start];
	}
	else if(start<end) {
		int mid = (start+end)/2;
		merge_sort(sarr,tarr,start,mid);
		merge_sort(sarr,tarr,mid+1,end);
		merge(tarr,sarr,start,mid,end);
	}

}

void merge(int sarr[],int tarr[],int start,int mid,int end) {
	int i,j,k;
	for(i=start,j=mid+1,k=start;j<=end && i<=mid;k++) {
		if(sarr[j]<sarr[i]) tarr[k] = sarr[j++];
		else tarr[k] = sarr[i++];
	}
	while(j<=end) tarr[k++] = sarr[j++];
	while(i<=mid) tarr[k++] = sarr[i++];

	for(i=start;i<=end;i++){
		sarr[i]=tarr[i];
	}
}

